package com.xx.account.service.impl;

import java.util.List;
import java.util.UUID;

import com.xx.account.domain.AccountWorker;
import com.xx.account.mapper.AccountWorkerMapper;
import com.xx.common.exception.ServiceException;
import com.xx.common.utils.DateUtils;
import com.xx.common.utils.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xx.account.mapper.AccountSendLeafletMapper;
import com.xx.account.domain.AccountSendLeaflet;
import com.xx.account.service.IAccountSendLeafletService;

import javax.annotation.Resource;

/**
 * 分配订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-29
 */
@Service
public class AccountSendLeafletServiceImpl implements IAccountSendLeafletService
{
    @Resource
    private AccountSendLeafletMapper accountSendLeafletMapper;

    @Resource
    private AccountWorkerMapper accountWorkerMapper;

    /**
     * 查询分配订单
     *
     * @param id 分配订单主键
     * @return 分配订单
     */
    @Override
    public AccountSendLeaflet selectAccountSendLeafletById(String id)
    {
        return accountSendLeafletMapper.selectAccountSendLeafletById(id);
    }

    /**
     * 查询分配订单列表
     *
     * @param accountSendLeaflet 分配订单
     * @return 分配订单
     */
    @Override
    public List<AccountSendLeaflet> selectAccountSendLeafletList(AccountSendLeaflet accountSendLeaflet)
    {
        return accountSendLeafletMapper.selectAccountSendLeafletList(accountSendLeaflet);
    }

    /**
     * 新增分配订单
     *
     * @param accountSendLeaflet 分配订单
     * @return 结果
     */
    @Override
    public int insertAccountSendLeaflet(AccountSendLeaflet accountSendLeaflet)
    {
//        int workerCount = accountSendLeafletMapper.selectAccountSendLeafletByRecordIdAndWorkerId(accountSendLeaflet.getId(),         accountSendLeaflet.getWorkerId());
//        if(workerCount > 0){
//            throw new ServiceException("当前订单已分配该投手");
//        }
        accountSendLeaflet.setId(UUID.randomUUID().toString().toString().replaceAll("-",""));

        // 工人操作次数+1
        AccountWorker accountWorker = accountWorkerMapper.selectAccountWorkerById(accountSendLeaflet.getWorkerId());
        accountWorker.setCount(accountWorker.getCount() + 1);
        accountWorker.setEndTime(DateUtils.getNowDate());
        accountWorkerMapper.updateAccountWorker(accountWorker);
        return accountSendLeafletMapper.insertAccountSendLeaflet(accountSendLeaflet);
    }

    /**
     * 修改分配订单
     *
     * @param accountSendLeaflet 分配订单
     * @return 结果
     */
    @Override
    public int updateAccountSendLeaflet(AccountSendLeaflet accountSendLeaflet)
    {
        return accountSendLeafletMapper.updateAccountSendLeaflet(accountSendLeaflet);
    }

    /**
     * 批量删除分配订单
     *
     * @param recordIds 需要删除的分配订单主键
     * @return 结果
     */
    @Override
    public int deleteAccountSendLeafletByIds(String[] recordIds)
    {
        return accountSendLeafletMapper.deleteAccountSendLeafletByIds(recordIds);
    }

}
